<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Validate;
use think\Session;

class Login extends Controller
{
    public function index()
    {
        return view('index');
    }
    public function login()
    {
        $data['username'] = input('post.username');
        $data['pass'] =md5(input('post.pass'));
        $code=input('post.code');
        $rule = [
            'username' => 'require',
            'pass' => 'require',
            ];
        $msg = [
            'username.require' => '用户名不能为空',
            'pass.require' => '密码不能为空',
            ];
        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if ($result!==true) {
            return $this->error($validate->getError());
        }
        if(!captcha_check($code)){
            return $this->error('验证码错误');
            
        };
        $link=Db::name('admin_user')->where($data)->find();
        if ($link) {
            Session::set('admin_user',$link);

            return $this->success('登入成功',"index/index");
        }else{
            return $this->error('登入失败');
        }
    }
    public function aout(){
        Session::delete('admin_user');
        return $this->success("退出成功!",'login/index');
    }
}
